/**
 *	@file	bc_use.c
 *	@brief	ボングリジュース使用シーケンスメイン
 *	@author	Miyuki Iwasawa
 *	@date	08.11.06
 *
 */

#include "common.h"
#include "system/procsys.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/snd_tool.h"
#include "system/fontproc.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/pmfprint.h"
#include "system/buflen.h"
#include "system/window.h"
#include "system/brightness.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/palanm.h"
#include "system/wipe.h"
#include "system/window.h"
#include "system/font_arc.h"
#include "system/winframe.naix"
#include "gflib/touchpanel.h"
#include "msgdata/msg.naix"

#include "savedata/config.h"
#include "application/bong_case.h"
#include "bc_sys.h"
#include "bc_use.h"
#include "bc_use_sub.h"
#include "bc_snd_def.h"

//=========================================================
//プロトタイプ定義
//=========================================================
static void app_InitData(BC_USEWORK* wk);
static void app_EndData(BC_USEWORK* wk);
static BOOL app_Init(BC_USEWORK* wk);
static BOOL app_End(BC_USEWORK* wk);

static int seq_FadeIn(BC_USEWORK* wk);
static int seq_FadeOut(BC_USEWORK* wk);
static int seq_DemoMain(BC_USEWORK* wk);
static int seq_UseEff(BC_USEWORK* wk);
static int seq_ResultCheck(BC_USEWORK* wk);

static void sub_ViewClear(void);

//=========================================================
//プロセス定義
//=========================================================
/**
 *	@brief	ボングリジュース使用デモ呼び出し
 */
PROC_RESULT BongUseDemoProc_Init(PROC* proc,int* seq)
{

	switch(*seq){
	case 0:
		sub_ViewClear();

		//ヒープ作成
		HeapStatePush();
		sys_CreateHeap(HEAPID_BASE_APP,HEAPID_BONGCASE_APP,0x90000);

		{
			BC_USEWORK* wk = NULL;
			wk = PROC_AllocWork(proc,sizeof(BC_USEWORK),HEAPID_BONGCASE_APP);
			MI_CpuClear8(wk,sizeof(BC_USEWORK));

			wk->heapID = HEAPID_BONGCASE_APP;
			wk->sp = (BC_SYSWORK*)PROC_GetParentWork(proc);

			//サウンド引継ぎ設定
//			Snd_DataSetByScene( SND_SCENE_SUB_JUICE, 0, 0 );

			//引継ぎデータ初期化
			app_InitData(wk);
		}
		(*seq)++;
		break;
	case 1:
		if(app_Init((BC_USEWORK*)PROC_GetWork(proc))){
			return PROC_RES_FINISH;
		}
	}
	return PROC_RES_CONTINUE;
}

/**
 *	@brief	ボングリボックスモジュールメイン
 */
PROC_RESULT BongUseDemoProc_Main(PROC* proc,int* seq)
{
	BC_USEWORK* wk = (BC_USEWORK*)PROC_GetWork(proc);

	switch(*seq){
	case SEQ_FADE_IN:
		*seq = seq_FadeIn(wk);
		break;
	case SEQ_FADE_OUT:
		*seq = seq_FadeOut(wk);
		break;
	case SEQ_MAIN:
		*seq = seq_DemoMain(wk);
		break;
	case SEQ_USE_EFF:
		*seq = seq_UseEff(wk);
		break;
	case SEQ_RESULT_CHECK:
		*seq = seq_ResultCheck(wk);
		break;
	case SEQ_EXIT:
		return PROC_RES_FINISH;
	}
	CATS_Draw(wk->pActRes);
	BongUse3D_Main(wk->p3d_wk);
	return PROC_RES_CONTINUE;
}

/**
 *	@brief	ボングリボックスモジュール終了
 */
PROC_RESULT BongUseDemoProc_End(PROC* proc,int* seq)
{
	BC_USEWORK* wk = (BC_USEWORK*)PROC_GetWork(proc);

	if(!app_End(wk)){
		return PROC_RES_CONTINUE;
	}
	//引継ぎデータ書き戻し
	app_EndData(wk);

	//ワーク解放
	PROC_FreeWork(proc);

	HeapStatePop();
	HeapStateCheck(HEAPID_BONGCASE_APP);

	sys_DeleteHeap(HEAPID_BONGCASE_APP);

	return PROC_RES_FINISH;
}

////////////////////////////////////////////////////////////////////////
//サブ

/**
 *	@brief	引継ぎデータ初期化
 */
static void app_InitData(BC_USEWORK* wk)
{
	int i;
	CONFIG* config;

	//キーモード初期化
	wk->key_mode = KeyTouchStatus_CheckTouchOrKey(wk->sp->param->kt_status);

	wk->save = wk->sp->param->save;

	//コンフィグ
	config = SaveData_GetConfig(wk->save);
	wk->msg_spd = CONFIG_GetMsgPrintSpeed(config);
	wk->win_type = CONFIG_GetWindowType(config);

	//ぼんぐりセーブデータ取得
	wk->bong_sv = SaveData_GetBonguriSave(wk->save);

	//ジュースデータ取得
	BonguriSave_GetJuiceParam(wk->bong_sv,wk->sp->juice_id,&wk->juice);
	BongJuice_GetFlavorParam(&wk->juice,&wk->flavor);

	//ポケモンデータ取得
	BCUseSub_GetPokeParam(wk);

	//初期ポジションセット
	for(i = 0;i < wk->poke_num;i++){
		if(wk->poke[i].idx == wk->sp->ret_poke){
			wk->pos = i;
			break;
		}
	}
}

/**
 *	@brief	引継ぎデータ終了処理
 */
static void app_EndData(BC_USEWORK* wk)
{
	//ポケモンデータ破棄
	BCUseSub_DelPokeParam(wk);

	//キーモード書き戻し
	KeyTouchStatus_SetTouchOrKey(wk->sp->param->kt_status,wk->key_mode);

	//ポケモン選択位置書き戻し
	wk->sp->ret_poke = wk->poke[wk->pos].idx;

	if(wk->use_f){
		wk->sp->mode = SYSMODE_POT;
	}else{
		wk->sp->mode = SYSMODE_PLIST;
	}
	if(wk->sp->param->ret_wk != NULL){
		*wk->sp->param->ret_wk = wk->use_f;
	}
}

/**
 *	@brief	初期化
 *
 */
static BOOL app_Init(BC_USEWORK* wk)
{
	switch(wk->seq){
	case 0:
		BongUseResInit(wk);
		++wk->seq;
		break;
	case 1:
		BongUseInitialDraw(wk);

		//タッチパネル初期化
		InitTPSystem();
		InitTPNoBuff(1);

		wk->seq = 0;
		return TRUE;
	}
	return FALSE;

}

/**
 *	@brief	終了処理
 *
 */
static BOOL app_End(BC_USEWORK* wk)
{
	switch(wk->seq){
	case 0:
		//タッチパネル終了
		StopTP();
		BongUseResRelease(wk);
		++wk->seq;
		break;
	case 1:
	default:
		sub_ViewClear();
		wk->seq = 0;
		return TRUE;
	}
	return FALSE;
}

/**
 *	@brief	フェードイン
 *
 */
static int seq_FadeIn(BC_USEWORK* wk)
{
	switch(wk->seq){
	case 0:
		WIPE_SYS_Start(
			WIPE_PATTERN_WMS, WIPE_TYPE_FADEIN,
			WIPE_TYPE_FADEIN, WIPE_FADE_BLACK, WIPE_DEF_DIV, WIPE_DEF_SYNC, wk->heapID);
		wk->seq++;
		break;
	case 1:
		if( !WIPE_SYS_EndCheck()){
			break;
		}
		wk->seq = 0;
		return SEQ_MAIN;
	}
	return SEQ_FADE_IN;
}

/**
 *	@brief	フェードアウト
 *
 */
static int seq_FadeOut(BC_USEWORK* wk)
{
	switch(wk->seq){
	case 0:
		WIPE_SYS_Start(
			WIPE_PATTERN_WMS, WIPE_TYPE_FADEOUT,
			WIPE_TYPE_FADEOUT, WIPE_FADE_BLACK, WIPE_DEF_DIV, WIPE_DEF_SYNC, wk->heapID);
		wk->seq++;
		break;
	case 1:
		if( !WIPE_SYS_EndCheck()){
			break;
		}
		wk->seq = 0;
		return SEQ_EXIT;
	}
	return SEQ_FADE_OUT;
}

/**
 *	@brief	デモ画面メイン
 */
static int seq_DemoMain(BC_USEWORK* wk)
{
	int ret = SEQ_MAIN;

	ret = BCUseSub_InputCheck(wk);
	return ret;
}

/**
 *	@brief	使うエフェクト再生
 */
static int seq_UseEff(BC_USEWORK* wk)
{
	switch(wk->seq){
	case 0:
		if(!BCUseSubSeq_UseEffect(wk)){
			break;
		}
		if(wk->diff_prm_num > 0){
			wk->seq = 0;
			wk->res_ct = 0;
			return SEQ_RESULT_CHECK;
		}
		wk->seq++;
		break;
	case 1:
		//なにも変わらなかったメッセージ表示
		if(!BCUseSubSeq_NoChangeMsg(wk)){
			break;
		}
		wk->seq = 0;
		return SEQ_FADE_OUT;
	}
	return SEQ_USE_EFF;
}


#include "bc_tbl.dat"

/**
 *	@brief	使った結果表示
 */
static int seq_ResultCheck(BC_USEWORK* wk)
{
	switch(wk->seq){
	case 0:
		for(;wk->res_ct < PM_PERFORMANCE_MAX;wk->res_ct++){
			// @@@
//			if(wk->diff_prm[ g_PerformancePosTbl[wk->res_ct] ] != 0){
			if(wk->diff_prm[ wk->res_ct ] != 0){
				break;
			}
		}
		wk->seq++;
	case 1:
		if(!BCUseSubSeq_ResultCheck(wk)){
			break;
		}
		wk->seq = 0;
		++wk->res_ct;
		if(--wk->diff_prm_num == 0){
			return SEQ_FADE_OUT;	//終了
		}
		break;
	}
	return SEQ_RESULT_CHECK;
}

/**
 *	@brief	画面初期化
 */
static void sub_ViewClear(void)
{
	sys_VBlankFuncChange(NULL, NULL);
	sys_HBlankIntrStop();	//HBlank割り込み停止

	GF_Disp_GX_VisibleControlInit();
	GF_Disp_GXS_VisibleControlInit();
	GX_SetVisiblePlane(0);
	GXS_SetVisiblePlane(0);

	WIPE_SetBrightness(WIPE_DISP_MAIN,WIPE_FADE_BLACK);
	WIPE_SetBrightness(WIPE_DISP_SUB,WIPE_FADE_BLACK);
	WIPE_ResetWndMask(WIPE_DISP_MAIN);
	WIPE_ResetWndMask(WIPE_DISP_SUB);
}
